\section{Computer Vision in Python}

\centeredlargetext{white}{black}{
Computer Vision in Python
}


\begin{frame}[fragile]
\frametitle{Python Modules for Computer Vision}
\begin{itemize}
 \item This tutorial will focus on directly using Python interfaces to large open source C++ projects:
 \begin{itemize}
   \item OpenCV
   \item ITK
 \end{itemize}
 \item Several packages for computer vision are built on top of some combination of NumPy, SciPy, OpenCV, and various other modules
  \begin{description}
    \item[SimpleCV] \url{http://simplecv.org/}
    \item[PyVision] \url{http://sourceforge.net/projects/pyvision/}
    \item[PyCVF]    \url{http://pycvf.sourceforge.net/}
    \item[PyCam]    \url{http://code.google.com/p/pycam/}
    \item[NDVision] \url{https://launchpad.net/ndvision}
  \end{description}
\end{itemize}
\end{frame}


\subsection{PIL: Python Image Library}

\begin{frame}[fragile]
\frametitle{Loading and Saving Images with PIL}
\begin{itemize}
  \item The Python Imaging Library (PIL) has many image manipulation functions.
  \item PIL is most useful for loading and saving images in a large variety of standard image file formats.
  \item PIL uses its own image class, but it is easily converted to/from NumPy arrays.
\end{itemize}
\begin{lstlisting}
import Image
import numpy as np

def load_image(file_name):
    """Read an image file and return a NumPy array."""
    return np.array(Image.open(file_name))

def save_image(data, file_name):
    """Save a NumPy array in an image file."""
    return Image.fromarray(data).save(file_name)
\end{lstlisting}
\end{frame}


\subsection{OpenCV in Python}

\begin{frame}
\frametitle{What is OpenCV?}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item C/C++ library with Python wrappers
  \item Open source
  \item Very active community
  \item Implementations of many computer vision algorithms
\end{itemize}
\end{frame}


\begin{frame}
\frametitle{OpenCV Functionality Overview}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item Graphical User Interface(GUI) - very easy procedural interface
  \item Input/Output - load/write images and movies in a variety of formats
  \item Image Processing - filtering,edges,histograms, etc.
  \item Machine Learning - SVMs, Decision Trees, 
  \item Features 
  \item Object Detection
  \item and MORE! (http://docs.opencv.org/)
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Importing}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item Two versions of wrappers: \lstpy{cv} and \lstpy{cv2}
  \item \lstpy{cv2} contains \lstpy{cv}
  \item \lstpy{cv2} is preferred because it natively uses \lstpy{numpy}
\end{itemize}
\lstset{title=Examples/opencv\_rand.py}
\lstlistingwithnumber{3}{4}{Examples/opencv_rand.py}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Display Random Numpy Array}
\overlayicon{Logo/opencv}
\begin{columns}[c]
\column{0.65\textwidth}
\lstset{title=Examples/opencv\_rand.py}
\lstlistingwithnumber{1}{10}{Examples/opencv_rand.py}
\column{0.25\textwidth}
\includegraphics[width=\linewidth]{ScreenShot/opencv-rand}
\end{columns}
\begin{itemize}
 \item Run \verb|Examples/opencv_rand.py|
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Display Random Numpy Array}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item Generate an array of random numbers with NumPy.
    \lstlistingwithnumber{7}{7}{Examples/opencv_rand.py}
  \pause
  \item Show the array as an image in an OpenCV GUI window.\\
        The string is both the window identifier and the window title.
    \lstlistingwithnumber{8}{8}{Examples/opencv_rand.py}
  \pause
  \item Wait for the user to press a key.\\
        Optionally this function can take a timeout in milliseconds.
    \lstlistingwithnumber{9}{9}{Examples/opencv_rand.py}
  \pause
  \item Close the window and free memory.
        This is not strictly required in this case because
        it happens automatically when the script terminates.
    \lstlistingwithnumber{10}{10}{Examples/opencv_rand.py}
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Image Processing}
\overlayicon{Logo/opencv}
\lstset{title=Examples/opencv\_hello.py}
\lstlistingwithnumber{1}{23}{Examples/opencv_hello.py}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Image Processing}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item Run \verb|Examples/opencv_hello.py Data/photo.jpg|
\end{itemize}
\begin{center}
  \includegraphics[width=0.7\linewidth]{ScreenShot/opencv-hello}
\end{center}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Image Processing}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item Use OpenCV to read the image file into a NumPy array
    \lstlistingwithnumber{14}{14}{Examples/opencv_hello.py}
  \pause
  \item Resize the image to $800\times600$ using bilinear interpolation
    \lstlistingwithnumber{16}{16}{Examples/opencv_hello.py}
  \pause
  \item Define a tuple to represent a color in BGR color space
    \lstlistingwithnumber{17}{17}{Examples/opencv_hello.py}
  \pause
  \item Draw text on the image in at location (200, 200) at a scale of 3.0
    \lstlistingwithnumber{18}{20}{Examples/opencv_hello.py}
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Movie Player}
\overlayicon{Logo/opencv}
\lstset{title=Examples/opencv\_movie.py}
\lstlistingwithnumber{1}{22}{Examples/opencv_movie.py}
\begin{itemize}
  \item Run \verb|Examples/opencv_movie.py Data/movie.mov|
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Movie Player}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item Open the video file
    \lstlistingwithnumber{14}{14}{Examples/opencv_movie.py}
  \pause
  \item Start an infinite loop for reading and displaying video frames
    \lstlistingwithnumber{15}{15}{Examples/opencv_movie.py}
  \pause
  \item Read one frame if available, if not exit the loop
    \lstlistingwithnumber{16}{18}{Examples/opencv_movie.py}
  \pause
  \item Display the image in a GUI window, and wait for 5 milliseconds
    \lstlistingwithnumber{19}{20}{Examples/opencv_movie.py}
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Feature Detection}
\overlayicon{Logo/opencv}
\lstset{title=Examples/opencv\_features.py}
\lstlistingwithnumber{1}{23}{Examples/opencv_features.py}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Feature Detection}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item Run \verb|Examples/opencv_features.py Data/photo.jpg|
\end{itemize}
\begin{center}
  \includegraphics[width=0.7\linewidth]{ScreenShot/opencv-features}
\end{center}
\end{frame}


\begin{frame}[fragile]
\frametitle{OpenCV By Example -- Feature Detection}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item Convert the color space from RGB to Gray.\\
        Feature detection requires a gray-scale image.
    \lstlistingwithnumber{16}{16}{Examples/opencv_features.py}
  \pause
  \item Run the ``Good Features to Track'' algorithm to find 50 points.\\
        The last two arguments are a threshold and minimum distance between points.
    \lstlistingwithnumber{17}{17}{Examples/opencv_features.py}
  \pause
  \item The features are a 3D array of shape (50, 1, 2).\\
        Reshape it to (50, 2).  The -1 means infer from data length.
    \lstlistingwithnumber{18}{18}{Examples/opencv_features.py}
  \pause
  \item At each feature location (x, y) draw a circle of radius 10 in red.
    \lstlistingwithnumber{19}{20}{Examples/opencv_features.py}
\end{itemize}
\end{frame}


\subsection{Exercise 3}

\begin{frame}[fragile]
\frametitle{Exercise 3}
\framesubtitle{OpenCV}
\overlayicon{Logo/opencv}
\begin{itemize}
  \item Open the file \verb|Exercises/ex3.py|.
  \item Modify the code to run \lstpy{goodFeaturesToTrack} on the movie.
  \item Then modify it to run \lstpy{Canny} edge detection instead.
\end{itemize}
\lstset{title=Exercises/ex3.py}
\lstlistingwithnumber{16}{24}{Exercises/ex3.py}
\end{frame}


\begin{frame}[fragile]
\frametitle{Exercise 3: Answer}
\framesubtitle{OpenCV}
\overlayicon{Logo/opencv}
\lstset{title=Exercises/Answers/ex3-answer1.py}
\lstlistingwithnumber{19}{24}{Exercises/Answers/ex3-answer1.py}
\lstset{title=Exercises/Answers/ex3-answer2.py}
\lstlistingwithnumber{19}{21}{Exercises/Answers/ex3-answer2.py}
\end{frame}


\subsection{SimpleITK in Python}
\input{section-python-vision-simpleitk}
